# == Schema Information
# Schema version: 22
#
# Table name: schools
#
#  id             :integer       not null, primary key
#  name           :string(255)   
#  field_of_study :string(255)   
#  start_date     :date          
#  end_date       :date          
#  city           :string(255)   
#  state          :string(255)   
#  user_id        :integer       
#  degree_id      :integer       
#

class School < ActiveRecord::Base
  belongs_to :degree
  belongs_to :user
  validates_presence_of :user, :degree, :field_of_study, :start_date, :city, :name
  validates_date :start_date, :end_date, :allow_nil => true
  before_validation :create_dates_from_years
  attr_accessor :start_year, :end_year

  private

  def create_dates_from_years
    self.start_date = Date.new(self.start_year.to_i) if self.start_year
    self.end_date = self.end_year.blank? ? nil : Date.new(self.end_year.to_i)
  end

end
